package com.zym.init.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zym.init.core.exception.ServiceException;
import com.zym.init.mapper.PermissionMapper;
import com.zym.init.mapper.RoleMapper;
import com.zym.init.model.dto.PageDTO;
import com.zym.init.model.po.PermissionPo;
import com.zym.init.model.po.RolePo;
import com.zym.init.model.query.PermissionQuery;
import com.zym.init.model.vo.PermissionVo;
import com.zym.init.service.PermissionService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zouyangming
 * @description 针对表【permission(权限表)】的数据库操作Service实现
 * @createDate 2024-05-27 22:06:03
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, PermissionPo> implements PermissionService {

    private final RoleMapper roleMapper;

    public PermissionServiceImpl(RoleMapper roleMapper) {
        this.roleMapper = roleMapper;
    }

    /**
     * 分页查询权限列表
     */
    @Override
    public PageDTO<PermissionVo> getPage(PermissionQuery query) {
        // 分页查询
        Page<PermissionPo> page = lambdaQuery()
                .eq(query.getPermissionId() != null, PermissionPo::getPermissionId, query.getPermissionId())
                .like(query.getPermissionName() != null, PermissionPo::getPermissionName, query.getPermissionName())
                .eq(query.getPermissionCode() != null, PermissionPo::getPermissionCode, query.getPermissionCode())
                .eq(query.getStatus() != null, PermissionPo::getStatus, query.getStatus())
                .page(query.toMpPageDefaultSortByCreateTimeDesc());
        // 封装返回
        return PageDTO.of(page, PermissionVo.class);
    }

    /**
     * 新增权限
     *
     * @param query 权限信息
     * @return 结果
     */
    @Override
    public boolean add(PermissionQuery query) {
        isPermissionCodeExist(query.getPermissionCode());
        PermissionPo permission = new PermissionPo();
        permission.setPermissionName(query.getPermissionName());
        permission.setPermissionCode(query.getPermissionCode());
        permission.setPermissionDesc(query.getPermissionDesc());
        permission.setStatus(query.getStatus());
        return save(permission);
    }

    /**
     * 编辑权限
     *
     * @param query 权限信息
     * @return 结果
     */
    @Override
    public boolean edit(PermissionQuery query) {
        QueryWrapper<PermissionPo> wrapper = new QueryWrapper<>();
        wrapper.eq("permission_id", query.getPermissionId());
        PermissionPo po = getOne(wrapper);
        if (po == null) {
            throw new ServiceException("权限不存在");
        }
        if (!po.getPermissionCode().equals(query.getPermissionCode())) {
            isPermissionCodeExist(query.getPermissionCode());
        }
        PermissionPo permission = new PermissionPo();
        permission.setPermissionId(query.getPermissionId());
        permission.setPermissionName(query.getPermissionName());
        permission.setPermissionDesc(query.getPermissionDesc());
        permission.setStatus(query.getStatus());
        return updateById(permission);
    }

    /**
     * 根据权限代码获取权限信息列表
     *
     * @param codes 权限代码列表
     * @return 权限信息列表
     */
    @Override
    public List<PermissionVo> getByCodeList(List<String> codes) {
        List<PermissionVo> list = new ArrayList<>();
        if (codes != null && !codes.isEmpty()) {
            list = BeanUtil.copyToList(lambdaQuery()
                    .in(PermissionPo::getPermissionCode, codes)
                    .list(), PermissionVo.class);
        }
        return list;
    }

    /**
     * 判断权限代码是否存在
     */
    private void isPermissionCodeExist(String permissionCode) {
        QueryWrapper<PermissionPo> wrapper = new QueryWrapper<>();
        wrapper.eq("permission_code", permissionCode);
        if (count(wrapper) > 0) {
            throw new ServiceException("权限代码已存在");
        }
    }

    /**
     * 判断权限代码是否被使用
     *
     * @param ids 权限ID列表
     * @return 是否被使用
     */
    @Override
    public boolean isPermissionCodeUsed(List<Long> ids) {
        QueryWrapper<PermissionPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("permission_id", ids);
        List<PermissionPo> permissionPoList = list(queryWrapper);
        List<String> permissionCodeList = new ArrayList<>();
        for (PermissionPo permissionPo : permissionPoList) {
            permissionCodeList.add(permissionPo.getPermissionCode());
        }
        return isCodeUsed(permissionCodeList);
    }



    /**
     * 判断权限代码是否被使用
     *
     * @param permissionCodeList 权限代码列表
     * @return 是否被使用
     */
    public boolean isCodeUsed(List<String> permissionCodeList) {
        QueryWrapper<RolePo> queryWrapper = new QueryWrapper<>();
        for (String s : permissionCodeList) {
            queryWrapper.apply("JSON_CONTAINS(permission_list, '\"" + s + "\"')");
        }
        return roleMapper.selectCount(queryWrapper) > 0;
    }
}




